<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

class SiteController extends Controller
{


    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        $table_name = \Yii::$app->request->get('table_name');
        $service_name = \Yii::$app->request->get('service_name');
        $data['method'] = \Yii::$app->request->get('method','GET');
        $data['redis_client_name'] = \Yii::$app->request->get('redis_client_name','redis');
        $data['root_path_name'] = \Yii::$app->request->get('root_path_name','');
        $data['gen_code'] = \Yii::$app->request->get('gen_code',0);
        $data['file_path'] = \Yii::$app->request->get('file_path','');
        $data['branch'] = \Yii::$app->request->get('branch','');
        $data['uri'] = \Yii::$app->request->get('uri','');
        $data['uri_const'] = \Yii::$app->request->get('uri_const','');
        $data['function_name'] = \Yii::$app->request->get('function_name','');
        $data['function_desc'] = \Yii::$app->request->get('function_desc','');
        $data['words_array'] = [];//表名单词拆分
        $data['service_name_array'] = [];//服务名单词拆分
        $data['model_name'] = "";//UserAuth
        $data['service_name'] = $service_name;//user.feed.base
        $data['service_name_key'] = "";//UserFeedBase
        $data['table_name'] = $table_name;//user_auth
        if ($table_name) {
            $data['words_array'] = explode('_', $table_name);
            foreach ($data['words_array'] as $word) {
                $data['model_name'] .= ucwords($word);
            }
            $data['table_name_uri'] = implode('/', $data['words_array']);
        }

        if ($service_name) {
            $data['service_name_array'] = explode('.', $service_name);
            $data['service_name_uri'] = implode('/', $data['service_name_array']);
            foreach ($data['service_name_array'] as $word) {
                $data['service_name_key'] .= ucwords($word);
            }
        }

        if ($data['file_path'] && !is_dir($data['file_path'])) {
            echo "<script>alert('program_file_path不是目录，请核对后再填写！');</script>";
        }

        //如果要生成代码，则把当前分之的数据清理干净
//        if ($data['gen_code']) {
//            //执行命令，提交代码
//            $cmd = "cd {$data['file_path']} && git checkout . && git clean -df";
//            exec($cmd,$res_exe);
//        }

        return $this->renderPartial('index',['data'=>$data]);
    }
}
